/*
Applys the rotational pull between every Particle.
*/

class Gravity {

  Particle one;
  Particle two;

  float gravity;
  float strength;

 
  float distance;

  Gravity() {
  } 

  void set_particle_one(Particle one) {
    this.one = one;
  }

  void set_particle_two(Particle two) {
    this.two = two;
  }

  void set_distance(float distance) {
    this.distance = distance;
  }

  void apply_gravity(Particle two) {
    this.two = two; 
    apply_gravity();
  }

  void apply_gravity(Particle one, Particle two) {
    this.one = one;
    this.two = two;
    apply_gravity();
  }

  void apply_gravity() {
    calculate_strength();
    apply_rotation();
  }

  /// dump all saved data
  void reset() {
    one = null;
    two  = null;
    strength = 0;
    gravity = 0;
  }

  void apply_rotation() {
    one.rotation = one.rotation + attract(one.rotation,one.x_loc,one.y_loc,two.x_loc,two.y_loc,strength);
  }



  float calculate_strength() {
    strength = ((distance*distance)*(one.mass+two.mass))*.001;
   
    return strength;
  }

  float attract(float o, float x1, float y1, float x2, float y2, float change) {
   
    float direction_of = direction_to(x1, y1, x2, y2);
   
    if(direction_of > o) {
      if( direction_of - o < 360 -direction_of + o) {
        return change;
      }
      else {
        return change*-1;
      }
    }
    if(direction_of < o) {
      if(  o - direction_of < 360-o+direction_of ) {
        return change*-1;
      }
      else {
        return change;
      }
    }  
    return 0;

  }

  float direction_to(float one_x_loc, float one_y_loc, float two_x_loc, float two_y_loc) {
    float a = degrees(atan2(one_y_loc - two_y_loc, one_x_loc- two_x_loc)) +180 ;
    return a;
   
  }
  
  
  
  
}

